package com.wjggy.core.action.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wjggy.core.action.view.PostsView;
import com.wjggy.core.action.view.UserView;
import com.wjggy.core.common.page.Page;
import com.wjggy.core.common.page.PageUtil;
import com.wjggy.core.common.springview.ViewUtil;
import com.wjggy.core.entities.User;
import com.wjggy.core.service.user.ManageUserService;

/**
 * microlite-sns1.0 × Class Name: MangerPageAction.java
 * <p>
 * Description: 后台登录界面管理
 * </p>
 * <p>
 * Copyright:Copyright (c) 2011
 * </p>
 * <p>
 * Company:
 * </p>
 * DateTime 2012-1-5 下午01:38:49
 * 
 * @author xiaofan.yu
 * @see
 * @since 1.6
 */
@Controller
@RequestMapping("/admin")
public class MangerPageAction {
	private List<String> datas;
	public static final int PAGER_PAGESIZE = 10;
	private List<String> back;
	private static final Logger log = Logger.getLogger(MangerPageAction.class);
	

	/**
	 * 
	 * describe:后台首页
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView findIndex(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		// 这里不新增用户 
		return ViewUtil.findView(UserView.ADMIN_INDEX_PATH);
	}

	/**
	 * 
	 * describe:后台首页
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/goPage")
	public ModelAndView goPage(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		// 这里不新增用户
		String page = request.getParameter("page");
		if (page == null) {
			return ViewUtil.findView(UserView.ADMIN_INDEX_PATH);
		}
		return ViewUtil.findView("/admin/" + page);
	}

	/**
	 * 
	 * @describe 首页主页面
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/main")
	public ModelAndView findMain(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		// 恢复原始数据
		back = new ArrayList<String>();
		for (int i = 1; i <= 156; i++) {
			back.add("字符串" + i);
		}
		this.datas = back;
		// 获取当前页号
		int pageNo = 1;
		String pageNoStr = request.getParameter("pageNo");
		if (null != pageNoStr && !pageNoStr.equals("")) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		// 获取数据
		String name = request.getParameter("name");
		if (null != name && !"".equals(name)) {
			List<String> temp = new ArrayList<String>();
			for (String str : datas) {
				if (str.contains(name)) {
					temp.add(str);
				}
			}
			this.datas = temp;
		}
		// 获取总记录数
		int recordCount = this.datas.size();
		// 获取分页数据
		int start = (pageNo - 1) * PAGER_PAGESIZE;
		int end = start + PAGER_PAGESIZE;
		if (end > this.datas.size()) {
			end = this.datas.size();
		}
		List<String> results = this.datas.subList(start, end);
		// 把分页数据和分页标签所需要的参数存放到request中
		Page page = new Page(results, PAGER_PAGESIZE, pageNo, recordCount,
				"main.htm");
//		System.out.println(results);
		ModelAndView mav = new ModelAndView();
		mav.addObject("page", page);
		// 这里不新增用户
		mav.setViewName("/defaul/admin/main");
		return mav;
	}
}
